/* eslint-disable no-undef */
import getRequest from '@/server/getConfig';

function GetHttpRequest() {
  if (window.XMLHttpRequest) {
    // Gecko
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // IE
    return new ActiveXObject('MsXml2.XmlHttp');
  }
}

function ajaxPage(sId, url) {
  const oXmlHttp = GetHttpRequest();
  oXmlHttp.onreadystatechange = function() {
    if (oXmlHttp.readyState === 4) {
      includeJS(sId, url, oXmlHttp.responseText);
    }
  };
  oXmlHttp.open('GET', url, false); // 同步操作
  oXmlHttp.send(null);
}

export function includeJS(sId, fileUrl, source) {
  if ((source != null) && (!document.getElementById(sId))) {
    const oHead = document.getElementsByTagName('HEAD').item(0);
    const oScript = document.createElement('script');
    oScript.type = 'text/javascript';
    oScript.id = sId;
    oScript.text = source;
    oHead.appendChild(oScript);
  }
}

(async function() {
  const { data: { showConsole }} = await getRequest();
  if (showConsole === true) {
    ajaxPage('vconsole', '/static/js/vconsole.js');
    new VConsole();
  }
})();

